public class Leet13 {
//	static int num = 0;
	static int a = 10;
	public static void main(String[] args) {
		
		Leet13 l = new Leet13();
		
		System.out.println(l.romanToInt(""));
	}
	public int romanToInt(String s) {
		return back(s,check(s));
    }
	static int back(String s,int at) {
		int num = 0;
		if(s.length()==1) {
			return value(s.charAt(0));
		}
		if (s.length()==0)
			return 0;
		num = value(s.charAt(at));
		num+=back(s.substring(at+1,s.length()),check(s.substring(at+1,s.length())));
		num-=back(s.substring(0,at),check(s.substring(0,at)));
		return num;
	}
	static int value(char t) {
		int num;
		num = t=='M'?1000:t=='D'?500:t=='C'?100:t=='L'?50:t=='X'?10:t=='V'?5:1;
		return num; 
	}
	static int check(String s) {
		for(int i = 0;i<s.length();i++) {
			if(s.charAt(i)=='M')
				return i ;
		}
		for(int i = 0;i<s.length();i++) {
			if(s.charAt(i)=='D')
				return i ;
		}
		for(int i = 0;i<s.length();i++) {
			if(s.charAt(i)=='C')
				return i ;
		}
		for(int i = 0;i<s.length();i++) {
			if(s.charAt(i)=='L')
				return i ;
		}
		for(int i = 0;i<s.length();i++) {
			if(s.charAt(i)=='X')
				return i ;
		}
		for(int i = 0;i<s.length();i++) {
			if(s.charAt(i)=='V')
				return i ;
		}
		for(int i = 0;i<s.length();i++) {
			if(s.charAt(i)=='I')
				return i ;
		}
		return -1;
	}
	

}
